package org.tree;
/*


 */
public class RBTree<K extends Comparable<K>,V> {

    private static final boolean RED = true;
    private static final boolean BLACK = false;

    private RBNode root;

    public RBTree() {
    }


    private boolean isRed(RBNode node){
        if(node!=null) {
            return node.color == RED ;
        }
        return false;
    }

    private boolean isBlack (RBNode node){
        if(node!=null) {
            return node.color == BLACK ;
        }
        return false;
    }

    private void setRed(RBNode node) {
        if(node !=null){
            node.color = RED ;
        }
    }

    private void setBlack(RBNode node) {
        if(node !=null){
            node.color = BLACK ;
        }
    }

    /**
     * 中序打印二叉树
     * @return
     */

    public void inOrderPrint() {
        inOrderPrint(this.root);
    }

    private void inOrderPrint(RBNode root) {
        if(root != null){
            inOrderPrint(root.left);
            System.out.println("key: " + root.key + "value: "+ root.value);
            inOrderPrint(root.left);
        }
    }


    static class RBNode<K extends Comparable<K>,V> {
        private RBNode parent;
        private RBNode left;
        private RBNode right;

        private K key;
        private V value;

        private boolean color;


        public RBNode(RBNode parent, RBNode left, RBNode right, K key, V value, boolean color) {
            this.parent = parent;
            this.left = left;
            this.right = right;
            this.key = key;
            this.value = value;
            this.color = color;
        }

        public RBNode() {
        }

        /**
         * 获取当前节点父亲节点
         * @return
         */

        private RBNode parentOf(RBNode node ){
            if(node != null){
                return node.parent;
            }
            return null;
        }

        /**
         * 节点是否是红色
         * @return
         */


        public RBNode getParent() {
            return parent;
        }

        public RBNode getLeft() {
            return left;
        }

        public RBNode getRight() {
            return right;
        }

        public K getKey() {
            return key;
        }

        public V getValue() {
            return value;
        }

        public boolean isColor() {
            return color;
        }

        public void setParent(RBNode parent) {
            this.parent = parent;
        }

        public void setLeft(RBNode left) {
            this.left = left;
        }

        public void setRight(RBNode right) {
            this.right = right;
        }

        public void setKey(K key) {
            this.key = key;
        }

        public void setValue(V value) {
            this.value = value;
        }

        public void setColor(boolean color) {
            this.color = color;
        }
    }



}
